package org.example.service;


import org.example.mapper.AwardMapper;
import org.example.mapper.MemberMapper;
import org.example.mapper.SettingMapper;
import org.example.model.Award;
import org.example.model.Member;
import org.example.model.Setting;
import org.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SettingService {

    @Autowired
    private SettingMapper settingMapper;

    @Autowired
    private AwardMapper awardMapper;

    @Autowired
    private MemberMapper memberMapper;

    public Setting query(User user) {
        //首先就是通过用户id查询设置信息
        Setting setting = queryByUserID(user.getId());

        //抽象设置返回给前端还需要包含user
        setting.setUser(user);

        //还要包含我们对应的奖项
        List<Award> awards = awardMapper.query(setting.getId());
        setting.setAwards(awards);

        //还需要包含我们对应的抽奖人员,通过设置id条件查询
        Member mParam = new Member();
        mParam.setSettingId(setting.getId());
        List<Member> members = memberMapper.selectByCondition(mParam);
        setting.setMembers(members);

        return setting;
    }

    public int insert(Setting setting) {
        return settingMapper.insertSelective(setting);
    }

    public Setting queryByUserID(Integer userId) {
        Setting param = new Setting();
        param.setUserId(userId);
        Setting setting = settingMapper.selectOne(param);
        return setting;
    }

    public int update(Setting setting) {
        return settingMapper.updateByPrimaryKeySelective(setting);
    }
}
